var prefix = ctx + "admin/user";
var isIE8 = false;

$(function() {
	$.common.initFormInfo();
	
	var userAgent = navigator.userAgent;
	var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; 
	var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(userAgent);
    var fIEVersion = parseFloat(RegExp["$1"]);
	if(isIE && fIEVersion == 8) {
		isIE8 = true
    }
	
	showIESwitch()

})
        
$("#editForm").validate({
	onkeyup : false,
	rules : {
		username : {
			required : true
		},
		deptName : {
			required : true
		},
		phone : {
			required : true,
			isPhone : true,
			remote : {
				type : "post",
				url : ctx + "admin/user/checkPhoneUnique",
				data : {
					userid: $("#userId").val(),
					isowner: $("#oper").val()=='add'?'false':'true',
					phone : function() {
						return $("#phone").val();
					}
				}
			}
		},
		email:{
			required:true,
            email:true,
            remote : {
				type : "post",
				url : ctx + "admin/user/checkEmailUnique",
				data : {
					userid: $("#userId").val(),
					isowner: $("#oper").val()=='add'?'false':'true',
					email : function() {
						return $("#email").val();
					}
				}
			}
		},
		loginname : {
			required : true,
			minlength : 2,
			remote : {
				type : "post",
				url : ctx + "admin/user/checkAccount",
				data : {
					isowner: $("#oper").val()=='add'?'false':'true',
					accountName : function() {
						return $("#loginname").val();
					}
				}
			}
		},
		password:{			
			minlength: 6
		}
	},
	messages : {
		username : {
			required : "请输入用户名",
		},
		deptName : {
			required : "请选择归属部门",
		},
		phone : {
			required : "请输入手机号码",
			isPhone : "请输入正确的手机号码",
			remote : "手机号码已经存在"
		},
		email: {
			required: "请输入邮箱",
			email: "请输入正确的邮箱",
			remote : "邮箱已经存在"
        },
		loginname : {
			required : "请输入登录名",
			minlength : "登录名不能小于2个字符",
			remote : "登录名已经存在"
		},
		password : {
			minlength : "密码不能小于6个字符"
		}
	},
	focusCleanup : true
});

/* 用户管理-修改-选择部门树 */
function selectDeptTree() {
	var deptId = $.common.isEmpty($("#treeId").val()) ? "" : $("#treeId").val();
    var url = ctx + "admin/dept/selectDeptTree?deptId=" + deptId;
    var options = {
		title: '选择部门',
		width: "380",
		url: url,
		callBack: doSubmit
	};
	$.modal.openOptions(options);
}

function doSubmit(index, layero){
	var tree = layero.find("iframe")[0].contentWindow.$._tree;
//	if ($.tree.notAllowParents(tree)) {
		var body = layer.getChildFrame('body', index);
		$("#treeId").val(body.find('#treeId').val());
		$("#treeName").val(body.find('#treeName').val());
		layer.close(index);
//	}
}

function submitHandler() {
    if ($.validate.form()) {
    	var status;
    	if (isIE8){
    		status = $("#toggle-switch").data('checked') == true ? 0 : 9;
    	}else{
    		status = $("input[id='status']").is(':checked') == true ? 0 : 9;
    	}
//    	var status = $("input[id='status']").is(':checked') == true ? 0 : 9;

    	$('#lockFlag').val(status);
    	var roleIds = $.form.selectCheckeds("role");
    	$('#roleIds').val(roleIds);
    	$.common.ajaxCommitForm($('#editForm'),true);
    	
//    	var ajaxFormOption = {
//   	    	 type: $("#oper").val()=='edit'?'PUT':'POST',
//   	    	 dataType : 'json',
//   	    	 beforeSubmit: function () {
//    	        	$.modal.loading("正在处理中，请稍后...");
//    	         },
//   	         success: function (result) { //提交成功的回调函数   
//   	             if(result.code==0){
//   	            	 if (result.code == web_status.SUCCESS) {
//   	                	var topWindow = $(window.parent.document);
//   	    	            var currentId = $('.page-tabs-content', topWindow).find('.active').attr('data-panel');
//   	    	            var $contentWindow = $('.RuoYi_iframe[data-id="' + currentId + '"]', topWindow)[0].contentWindow;
//   	    	            $.modal.close();
//   	    	            $contentWindow.$.modal.msgSuccess(result.msg==null?'操作成功':result.msg);
//   	    	            $contentWindow.$(".layui-layer-padding").removeAttr("style");
//   	    	            if ($contentWindow.table.options.type == table_type.bootstrapTable) {
//   	    	        		$contentWindow.$.table.refresh();
//   	    	        	} else if ($contentWindow.table.options.type == table_type.bootstrapTreeTable) {
//   	    	        		$contentWindow.$.treeTable.refresh();
//   	                    }
//   	    	            $.modal.closeUserTab();
//   	                } else if (result.code == web_status.WARNING) {
//   	                    $.modal.alertWarning(result.msg)
//   	                } else {
//   	                    $.modal.alertError(result.msg);
//   	                }
//   	                $.modal.closeLoading();
//   	             }
//   	             else{
//   	            	 $.modal.alertError(result.msg);
//   	             }
//   	             $.modal.closeLoading();
//   	         }
//   	    };
//    	$('#editForm').ajaxSubmit(ajaxFormOption);
    	
    	
    	return false;
    }
}

function showIESwitch(){
	//ie8 用户状态开关按钮
    if(isIE8) {
    	$("#toggle-switch").show()
    	$("#toggle-switch-normal").hide()
    	var firstCheckedFlag = $("#toggle-switch").data('checked')
    	if(firstCheckedFlag){
    		$("#toggle-switch").addClass('fa-toggle-on').removeClass('fa-toggle-off')
    	}else{
    		$("#toggle-switch").addClass('fa-toggle-off').removeClass('fa-toggle-on')
    	}
    	
    	$("#toggle-switch").click(function(){
    		var checkedFlag = $(this).data('checked')
    		$(this).data('checked',!checkedFlag)
    		console.log(!checkedFlag)
    		if($(this).hasClass('fa-toggle-off')){
    			$(this).addClass('fa-toggle-on').removeClass('fa-toggle-off')
    		}else{
    			$(this).addClass('fa-toggle-off').removeClass('fa-toggle-on')
    		}
    	})
    } else{
    	$("#toggle-switch").hide()
    	$("#toggle-switch-normal").show()
    }
}